<template>
    <div id="login">
        <div class="fa fa-chevron-left" id="back" @click="backClick"></div>
        <div id="login-title">发布新品</div>
        <input type="text" placeholder="请输入商品名称" v-model="nameOfGoods">
        <input type="text" placeholder="请输入商品价格" v-model.number="price">

        <div>请选择上传类型</div>
        
        <Picker :data="choose" :fixed-columns="3" :columns="3" v-model="value"></Picker>
        <div id="img">
            <div id="img-list" ref="imgList">
            <label for="file-selector" id="file-label" ref="fileLabel"><span class="fa fa-plus"><br> 上传图片</span></label>
            <input type="file" id="file-selector" accept="image/jpeg,image/png,image/gif" @change="chooseImage" multiple>
        </div>
        <div class="tips">每次最多上传五张哦</div>
        <button @click="upClick">上传</button>
        <router-link to="/">去首页</router-link>
        <Toast v-model="launchDones" v-if="launchThen">{{launchThen.msg}}</Toast>
        </div>
        <div>{{launchDone}}</div>  
    </div>
</template>

<script>
import {Picker,Toast} from "vux"
import {mapState} from "vuex";
export default {
    components: {
    Picker,  
    Toast,
},
computed: {
    ...mapState([
            "launchThen",
            "choose",
        ]),
        launchDone:function(){
            this.launchDones = this.$store.state.launchDone;
            setTimeout(()=>{
                this.$store.commit("setLaunchDone")
            },1000)
        }
},
mounted:function () {
    this.$store.dispatch("requestChoose");  
},
methods:{
        backClick:function(){
            history.back();
        },
        upClick:function(){
            var data = new FormData();
            for(var i=0;i<this.files.length;i++){
                data.append("pic",this.files[i]);

            }
            data.append("nameOfGoods",this.nameOfGoods);
            data.append("price",this.price);
            data.append("value",this.value);
            this.$store.dispatch("requestLaunch",data);
        },
        chooseImage:function(e){
            for(var i=0;i<e.target.files.length;i++){
                if(this.count<5){
                    this.count++;
                    this.files.push(e.target.files[i]);
                    var img = document.createElement("img");
                    img.style.width = "28vw";
                    img.style.width = "28vw";
                    var url = URL.createObjectURL(e.target.files[i]);
                    img.src = url;
                    this.$refs.imgList.insertBefore(img,this.$refs.fileLabel);
                }else{
                    return;
                }
                
            }
        }
    },
    data:function(){
        return {
            
            launchDones:false,
            nameOfGoods:null,
            price:null,
            files:[],
            value:[],
            count:0,
            classify:[
                
                {
                    name:"男装",
                    value:"男装",
                    parent:0,
                },
                {
                    name:"女装",
                    value:"女装",
                    parent:0,
                },
                {
                    name:"休闲裤",
                    value:"休闲裤",
                    parent:"男装",
                },
                {
                    name:"男士内搭",
                    value:"男士内搭",
                    parent:"男装",
                },
                {
                    name:"男士外套",
                    value:"男士外套",
                    parent:"男装",
                },
                {
                    name:"裙装",
                    value:"裙装",
                    parent:"女装",
                },
                {
                    name:"上装",
                    value:"上装",
                    parent:"女装",
                },
                {
                    name:"下装",
                    value:"下装",
                    parent:"女装",
                },
                {
                    name:"帽子",
                    value:"帽子",
                    parent:"女装",
                },
                {
                    name:"春季休闲裤",
                    value:"春季休闲裤",
                    parent:"休闲裤",
                },
                {
                    name:"夏季休闲裤",
                    value:"夏季休闲裤",
                    parent:"休闲裤",
                },
                {
                    name:"秋季休闲裤",
                    value:"秋季休闲裤",
                    parent:"休闲裤",
                },
                {
                    name:"冬季休闲裤",
                    value:"冬季休闲裤",
                    parent:"休闲裤",
                },
                {
                    name:"衬衫",
                    value:"衬衫",
                    parent:"男士内搭",
                },
                {
                    name:"长袖",
                    value:"长袖",
                    parent:"男士内搭",
                },
                {
                    name:"短袖",
                    value:"短袖",
                    parent:"男士内搭",
                },
                {
                    name:"春季外套",
                    value:"春季外套",
                    parent:"男士外套",
                },
                {
                    name:"秋季外套",
                    value:"秋季外套",
                    parent:"男士外套",
                },
                {
                    name:"冬季外套",
                    value:"冬季外套",
                    parent:"男士外套",
                },
                {
                    name:"春季裙装",
                    value:"春季裙装",
                    parent:"裙装",
                },
                {
                    name:"夏季裙装",
                    value:"夏季裙装",
                    parent:"裙装",
                },
                {
                    name:"秋季裙装",
                    value:"秋季裙装",
                    parent:"裙装",
                },
                {
                    name:"春季上装",
                    value:"春季上装",
                    parent:"上装",
                },
                {
                    name:"夏季上装",
                    value:"夏季上装",
                    parent:"上装",
                },
                {
                    name:"秋季上装",
                    value:"秋季上装",
                    parent:"上装",
                },
                {
                    name:"春季下装",
                    value:"春季下装",
                    parent:"下装",
                },
                {
                    name:"夏季下装",
                    value:"夏季下装",
                    parent:"下装",
                },
                {
                    name:"秋季下装",
                    value:"秋季下装",
                    parent:"下装",
                },
                {
                    name:"帽子",
                    value:"帽子",
                    parent:"帽子",
                },
                {
                    name:"白帽子",
                    value:"白帽子",
                    parent:"帽子",
                },
                {
                    name:"黑帽子",
                    value:"黑帽子",
                    parent:"帽子",
                },
                
            ]
        }
    },
    
}
</script>

<style scoped>
#login{
    background-color: #cccccc;
    height: 100vh;
    text-align: center;
}
#login{
    background-color: #cccccc;
    height: 100vh;
    text-align: center;
}
#login-title{
    text-align: center;
    font-size: 15px;
    height: 45px;
    line-height: 45px;
    background-color: skyblue;
    font-family: 宋体;
    font-weight: 700;
}
#back{
    position: absolute;
    height: 4px;
    line-height: 45px;
    left: 15px;
}
input{
height: 45px;
width: 95%;
margin-left: 4px;
margin-top: 10px;
margin-bottom: 10px;
padding: 10px;
}
#classify{
    margin: 10px;
}
#file-selector{
    display: none;
    }
#file-label{
    width: 28vw;
    height: 28vw;
    border: solid 1px black;
    display: inline-block;
    font-size: 40;
    text-align: center;
    line-height: 100px;
    vertical-align: top;
    }

.images{
    width: 28vw;
    height: 28vw;
    border: solid 1px black;
    margin-right: 6px;
    }
    #img{
        background-color: #999999;
    }
button{
    height: 45px;
    width: 98%;
    /*margin-left: 8px;*/
    margin-top: 10px;
    padding: 10px;
    background-color: darkturquoise;
    border-radius: 5px;
}
</style>